package com.scbo.activity;

import java.util.ArrayList;
import java.util.List;
import java.util.zip.Inflater;

import com.scbo.DTO.Buildorder;
import com.scbo.database.ScboDAO;
import com.scbo.main.R;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class ListBuildOrderActivity extends Activity {
	private int secondChoice;
	private Context mContext;
	private TextView tvBasic;
	private TextView tvMarco;
	private TextView tvAgr;
	private TextView tvAllin;
	private ScboDAO datasource;
	
	private String basic = " Basic Openers (For Bronze-Silver players):";
	private String marco = " Macro Builds:";
	private String aggressive = " Aggressive Builds:";
	private String allin = " All-in Builds:";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_list_bo);
		datasource = new ScboDAO(this);
		datasource.open();
		setUp();

	}
    @Override
    protected void onResume() {
    	datasource.open();
    	super.onResume();
    }
    @Override
    protected void onPause() {
    	datasource.close();
    	super.onPause();
    }
	private void setUp() {
		mContext = this;
		Bundle extra = getIntent().getExtras();
		secondChoice = extra.getInt("secondChoice");

		tvBasic = (TextView) findViewById(R.id.tv_basic_opener);
		tvMarco = (TextView) findViewById(R.id.tv_marco_build);
		tvAgr = (TextView) findViewById(R.id.tv_agg_build);
		tvAllin = (TextView) findViewById(R.id.tv_all_in_build);

		String coreType = "?";
		if (secondChoice == ChoseVsActivity.TvsT
				|| secondChoice == ChoseVsActivity.TvsP
				|| secondChoice == ChoseVsActivity.TvsZ) {

			if (secondChoice == ChoseVsActivity.TvsT) {
				coreType = "- TvsT";
			} else if (secondChoice == ChoseVsActivity.TvsP) {
				coreType = "- TvsP";
			} else if (secondChoice == ChoseVsActivity.TvsZ) {
				coreType = "- TvsZ";
			}
		} else if (secondChoice == ChoseVsActivity.PvsT
				|| secondChoice == ChoseVsActivity.PvsP
				|| secondChoice == ChoseVsActivity.PvsZ) {

			if (secondChoice == ChoseVsActivity.PvsT) {
				coreType = "- PvsT";
			} else if (secondChoice == ChoseVsActivity.PvsP) {
				coreType = "- PvsP";

			} else if (secondChoice == ChoseVsActivity.PvsZ) {
				coreType = "- PvsZ";

			}
		} else if (secondChoice == ChoseVsActivity.ZvsT
				|| secondChoice == ChoseVsActivity.ZvsP
				|| secondChoice == ChoseVsActivity.ZvsZ) {

			if (secondChoice == ChoseVsActivity.ZvsT) {
				coreType = "- ZvsT";

			} else if (secondChoice == ChoseVsActivity.ZvsP) {
				coreType = "- ZvsP";

			} else if (secondChoice == ChoseVsActivity.ZvsZ) {
				coreType = "- ZvsZ";

			}
		}

		tvBasic.setText(coreType + basic);
		tvMarco.setText(coreType + marco);
		tvAgr.setText(coreType + aggressive);
		tvAllin.setText(coreType + allin);

		LayoutInflater inflater = (LayoutInflater) getApplicationContext()
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		LinearLayout list1 = (LinearLayout) findViewById(R.id.list1);
		List<Buildorder> listBuild1 = datasource.getBoByTypeMatchTypMode(secondChoice, MainActivity.StandardBuilds);
		if (listBuild1 != null) {
			for (int i = 0; i < listBuild1.size(); i++) {
				final Buildorder buildOrder = listBuild1.get(i);
				final View itemOfList = inflater.inflate(R.layout.item_tv, null);
				itemOfList.setId(buildOrder.getId());
				itemOfList.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						Log.d("KHANHVQ", "SECOND CHOICE =" + secondChoice +", typeMode = " + buildOrder.getTypeMode());
						Intent i = new Intent(getApplicationContext(),
								TrainActivity.class);
						i.putExtra("buildOrder", buildOrder);
						startActivity(i);
					}
				});
				TextView tvItem = (TextView) itemOfList;
				tvItem.setText(buildOrder.getName());
				list1.addView(itemOfList);

			}
		}
		
		// for (int i = 0; i < 10; i++) {
		// final View itemOfList = inflater.inflate(R.layout.item_tv, null);
		// itemOfList.setId(i);
		// itemOfList.setOnClickListener(new OnClickListener() {
		//
		// @Override
		// public void onClick(View v) {
		// Toast.makeText(getApplicationContext(),
		// "msg msg: " + itemOfList.getId(),
		// Toast.LENGTH_SHORT).show();
		// }
		// });
		// TextView tvItem = (TextView) itemOfList;
		// tvItem.setText("Innovation's Marine/Hellion Expand");
		// list1.addView(itemOfList);
		//
		// }
		LinearLayout list2 = (LinearLayout) findViewById(R.id.list2);
		List<Buildorder> listBuild2 = datasource.getBoByTypeMatchTypMode(secondChoice, MainActivity.MarcoBuilds);
		if (listBuild2 != null) {
			for (int i = 0; i < listBuild2.size(); i++) {
				final Buildorder buildOrder = listBuild2.get(i);
				final View itemOfList = inflater.inflate(R.layout.item_tv, null);
				itemOfList.setId(buildOrder.getId());
				itemOfList.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						Log.d("KHANHVQ", "SECOND CHOICE =" + secondChoice +", typeMode = " + buildOrder.getTypeMode());
						Intent i = new Intent(getApplicationContext(),
								TrainActivity.class);
						i.putExtra("buildOrder", buildOrder);
						startActivity(i);
					}
				});
				TextView tvItem = (TextView) itemOfList;
				tvItem.setText(buildOrder.getName());
				list2.addView(itemOfList);

			}
		}
		LinearLayout list3 = (LinearLayout) findViewById(R.id.list3);
		List<Buildorder> listBuild3 = datasource.getBoByTypeMatchTypMode(secondChoice, MainActivity.AggressiveBuilds);
		if (listBuild3 != null) {
			for (int i = 0; i < listBuild3.size(); i++) {
				final Buildorder buildOrder = listBuild3.get(i);
				final View itemOfList = inflater.inflate(R.layout.item_tv, null);
				itemOfList.setId(buildOrder.getId());
				itemOfList.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						Log.d("KHANHVQ", "SECOND CHOICE =" + secondChoice +", typeMode = " + buildOrder.getTypeMode());
						Intent i = new Intent(getApplicationContext(),
								TrainActivity.class);
						i.putExtra("buildOrder", buildOrder);
						startActivity(i);
					}
				});
				TextView tvItem = (TextView) itemOfList;
				tvItem.setText(buildOrder.getName());
				list3.addView(itemOfList);

			}
		}
		LinearLayout list4 = (LinearLayout) findViewById(R.id.list4);
		List<Buildorder> listBuild4 = datasource.getBoByTypeMatchTypMode(secondChoice, MainActivity.AllInBuilds);
		if (listBuild4 != null) {
			for (int i = 0; i < listBuild4.size(); i++) {
				final Buildorder buildOrder = listBuild4.get(i);
				final View itemOfList = inflater.inflate(R.layout.item_tv, null);
				itemOfList.setId(buildOrder.getId());
				itemOfList.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						Log.d("KHANHVQ", "SECOND CHOICE =" + secondChoice +", typeMode = " + buildOrder.getTypeMode());
						Intent i = new Intent(getApplicationContext(),
								TrainActivity.class);
						i.putExtra("buildOrder", buildOrder);
						startActivity(i);
					}
				});
				TextView tvItem = (TextView) itemOfList;
				tvItem.setText(buildOrder.getName());
				list4.addView(itemOfList);

			}
		}
	}
	// @Override
	// public boolean onCreateOptionsMenu(Menu menu) {
	// // Inflate the menu; this adds items to the action bar if it is present.
	// getMenuInflater().inflate(R.menu.tvs, menu);
	// return true;
	// }
	

}
